<template>
  <div class="app-container">
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="700px" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="auto">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="损失原因编码" prop="lossCode">
              <el-input v-model="formData.lossCode" placeholder="请输入损失原因编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="损失原因名称" prop="lossName">
              <el-input v-model="formData.lossName" placeholder="请输入损失原因名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="父级" prop="parentId">
              <el-select v-model="formData.parentId" placeholder="请选择父级" style="width: 100%">
                <el-option  :key="0" label="无" value="0" />
                <el-option v-for="item in parentList" :key="item.id" :label="item.lossName" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="formData.status">
                <el-radio label="1">启用</el-radio>
                <el-radio label="0">禁用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="formData.remark" placeholder="请输入备注" type="textarea" :rows="3" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as LossReasonApi from '@/api/lamp/lossreason';
export default {
  name: "LossReasonForm",
  components: {},
  data() {
    return {
      dialogTitle: "",
      dialogVisible: false,
      formLoading: false,
      parentList: [],
      formData: {
        id: undefined,
        lossCode: undefined,
        lossName: undefined,
        parentId: '0', // 设置默认无父级
        remark: undefined,
        status: '1', // 已设置默认启用
      },
      formRules: {
        lossCode: [{ required: true, message: '损失原因编码不能为空', trigger: 'blur' }],
        lossName: [{ required: true, message: '损失原因名称不能为空', trigger: 'blur' }],
      },
    };
  },
  created() {
    this.getParentList();
  },
  methods: {
    async getParentList() {
      try {
        const res = await LossReasonApi.getLossReasonPage({ parentId: 0, pageNo: 1, pageSize: 100 });
        this.parentList = res.data.list;
      } catch (error) {
        this.$modal.msgError('获取父级列表失败');
      }
    },
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      if (id) {
        this.formLoading = true;
        try {
          const res = await LossReasonApi.getLossReason(id);
          this.formData = res.data;
          this.dialogTitle = "修改损失原因";
        } finally {
          this.formLoading = false;
        }
      } else {
        this.dialogTitle = "新增损失原因";
      }
    },
    async submitForm() {
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = { ...this.formData };
        if (data.parentId === undefined || data.parentId === null) {
          data.parentId = 0;
        }
        if (data.id) {
          await LossReasonApi.updateLossReason(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        await LossReasonApi.createLossReason(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    reset() {
      this.formData = {
        id: undefined,
        lossCode: undefined,
        lossName: undefined,
        parentId: '0', // 重置时也默认无父级
        remark: undefined,
        status: '1', // 重置时也默认启用
      };
      this.resetForm("formRef");
    }
  }
};
</script>
